package com.bawei.live.fragment

import android.app.AlertDialog
import android.os.CountDownTimer
import android.view.MotionEvent
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.base.RoutePath
import com.bawei.common.mvi.BaseMviFragment
import com.bawei.live.R
import com.bawei.live.adapter.GoodsSaleAdapter
import com.bawei.live.databinding.FragmentGoodsSaleBinding
import com.bawei.live.entity.UserEntity
import com.blankj.utilcode.util.ToastUtils


@Route(path = RoutePath.PATH_FRAGMENT_GOODS_SALE_COMMEND)
class GoodsSaleFragment : BaseMviFragment<FragmentGoodsSaleBinding>() {

    private lateinit var countDownTimer: CountDownTimer //实现倒计时
    private val totalTimeInMillis:Long = 60 * 60 * 1000 //60:00 转换为毫米哦

    private lateinit var mGoodsSaleAdapter:GoodsSaleAdapter
    private var mGoodsList = mutableListOf<UserEntity>()

    override fun getLayoutId(): Int =R.layout.fragment_goods_sale



    override fun initViews() {
        CountDownTime()

        mGoodsList.add(UserEntity(0,1,"","条条哥超级买手","鞋靴箱包",6609,false))
        mGoodsList.add(UserEntity(0,2,"","萝莉女装","服饰内衣",4502,false))
        mGoodsList.add(UserEntity(0,3,"","五月天","食品饮料",2721,false))
        mGoodsList.add(UserEntity(0,4,"","产品研究所","八维研究院",1923,false))
        mGoodsList.add(UserEntity(0,5,"","猪猪侠","二次元",1723,false))
        mGoodsList.add(UserEntity(0,6,"","你的欲梦","纯欲天花板",666,false))
        mGoodsList.add(UserEntity(0,7,"","程雨欣","美天花板",555,false))
        mGoodsList.add(UserEntity(0,8,"","猫小乐","不认识",444,false))
        mGoodsList.add(UserEntity(0,9,"","狗小七","+1",123,false))
        mGoodsList.add(UserEntity(0,10,"","章若楠","美女",121,false))
        mGoodsList.add(UserEntity(0,11,"","白梦妍","美女",12,false))
        mGoodsList.add(UserEntity(0,12,"","高启强","京海",4,false))
        mGoodsList.add(UserEntity(0,13,"","大小姐","没",2,false))


        mGoodsSaleAdapter = GoodsSaleAdapter(mGoodsList)

        mDataBinding.rvFlashGoods.run {
            layoutManager = LinearLayoutManager(requireContext(),LinearLayoutManager.VERTICAL,false)
            swapAdapter(mGoodsSaleAdapter,true)
        }

        mGoodsSaleAdapter.setOnItemClickListener(object:GoodsSaleAdapter.OnItemClickListener{
            override fun onItemClick(v: View, positions: Int) {
                if(positions!=0){
                    mDataBinding.textView.text = mGoodsList[positions].textCount.toString()
                    mDataBinding.textView2.text = mGoodsList[positions].userName
                    mDataBinding.textView4.text = "${mGoodsList[positions].hotDegree}热度"

                    mDataBinding.tvChaHot.text = "${mGoodsList[positions-1].hotDegree - mGoodsList[positions].hotDegree}热度"
                    showAlerTDialog(mGoodsList[positions])
                }else{
                    ToastUtils.showLong("当前直播间不予跳进")
                }
            }
        })





        mDataBinding.rulesType.setOnClickListener{
            mDataBinding.cltAwardRules.visibility = View.VISIBLE
            mDataBinding.cltGoods.visibility = View.GONE
            mDataBinding.rvFlashGoods.visibility = View.GONE
        }

        mDataBinding.ivGoBack.setOnClickListener{
            mDataBinding.cltAwardRules.visibility = View.GONE
            mDataBinding.cltGoods.visibility = View.VISIBLE
            mDataBinding.rvFlashGoods.visibility = View.VISIBLE
        }




    }

    private fun CountDownTime(){
        countDownTimer = object:CountDownTimer(totalTimeInMillis,1000){
            override fun onTick(p: Long) {
                val minutes = p / (60 * 1000)
                val seconds = (p % (60 * 1000))/1000
                val timeFormat = String.format("%02d:%02d", minutes, seconds)
                mDataBinding.tvDownTime.text = "倒计时:"+timeFormat
            }

            override fun onFinish() {
                ToastUtils.showLong("结束")
            }
        }.start()

    }

    private fun showAlerTDialog(list: UserEntity){

        val builder = AlertDialog.Builder(context)

        builder.setMessage("确定要跳转到 "+list.userName+" 的直播间么?")

        builder.setPositiveButton("确定并不在提醒"){dialog,which->
            dialog.dismiss()//关闭
        }
        builder.setNegativeButton("取消"){dialog,which->
            dialog.dismiss()
        }
        val dialog:AlertDialog = builder.create()
        dialog.show()
    }

}